package mq.demo2;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class TopicProducerDemo {
    public static void main(String[] args) throws JMSException {
        String brokerURL="tcp://192.168.100.100:61616";
        // 工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
        // 连接对象
        Connection connection = activeMQConnectionFactory.createConnection("admin", "admin");
        // 连接
        connection.start();
        // 第一个参数: 是否开启事务,第二个参数:应答模式
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic testTopic = session.createTopic("test_topic");
        // 创建生产者
        MessageProducer producer = session.createProducer(testTopic);
        // 创建消息
        TextMessage message = session.createTextMessage("topic测试消息1");
        // 发送消息
        producer.send(message);

        // 释放资源
        producer.close();
        session.close();
        connection.close();
    }
}
